<!-- 注意事项：这里面的表单里的页面结构一个比一个变态，不是我想这么搞，是UI奇怪要求和Element UI实在很不通用 -->
<template>
  <div class="login-container">
    <div class="title">{{ customerDesc || "就业创业职业大全云平台" }}</div>
    <div class="login-block">
      <!-- 登录表单 -->
      <div class="login-form">
        <!-- 角色切换 -->
        <div class="role-switch">
          <div
            :class="{ 'role-item': true, active: role == 'student' }"
            @click="changeRole('student')"
          >
            我是学生
          </div>
          <div
            :class="{ 'role-item': true, active: role == 'teacher' }"
            @click="changeRole('teacher')"
          >
            我是老师
          </div>
        </div>
        <div class="form-info">
          <div class="logo">
            <img v-if="logoSrc" :src="logoSrc" alt="" v-show="logoSrc" />
            <div v-else class="default-logo-container">
              <img class="default-logo" :src="defaultLogoSrc" alt="" />
              <div class="logo-text">{{ customerName || "职业大全" }}</div>
            </div>
          </div>
          <div class="descContainer">
            <span class="desc">{{ formDesc }}</span>
            <div
              class="otherEntrance"
              v-if="
                (role == 'student' && formType == 'login') ||
                (role == 'teacher' && SaaSformType == 'login')
              "
              @click="changeForm('smslogin')"
            >
              短信登录
            </div>
            <div
              class="otherEntrance"
              v-else-if="
                (role == 'student' && formType == 'smslogin') ||
                (role == 'teacher' && SaaSformType == 'smslogin')
              "
              @click="changeForm('login')"
            >
              密码登录
            </div>
          </div>
        </div>
        <template v-if="role == 'student'">
          <component :is="currentForm"></component>
        </template>
        <template v-if="role == 'teacher'">
          <component :is="currentSaaSForm"></component>
        </template>
      </div>
      <!-- 悬浮元素 -->
      <div class="login-flow">
        <img
          class="flow-item left top"
          src="https://img3.job1001.com/data_center/images/loginFlowLeftTop.png"
        />
        <img
          class="flow-item left bottom"
          src="https://img3.job1001.com/data_center/images/loginFlowLeftBottom.png"
        />
        <img
          class="flow-item right top"
          src="https://img3.job1001.com/data_center/images/loginFlowRightTop3.png"
        />
        <img
          class="flow-item right bottom"
          src="https://img3.job1001.com/data_center/images/loginFlowRightBottom2.png"
        />
      </div>
    </div>
  </div>
</template>

<script>
import { loginCustomerInfo } from "@/api/user";
import registerForm from "@/views/login/components/registerForm.vue";
import loginForm from "@/views/login/components/loginForm.vue";
import forgetForm from "@/views/login/components/forgetForm.vue";
import smsloginForm from "@/views/login/components/smsloginForm.vue";
import SaaSloginForm from "@/views/login/components/SaaSloginForm.vue";
import SaaSForgetForm from "@/views/login/components/SaaSForgetForm.vue";
import SaaSsmsloginForm from "@/views/login/components/SaaSsmsloginForm.vue";
export default {
  name: "Login",
  data() {
    return {
      currentSaaSForm: SaaSloginForm,
      currentForm: loginForm,
      customerName: "", //学校名称
      customerDesc: "",
      formDesc: "",
      logoSrc: "",
      defaultLogoSrc:
        "https://zt.crm1001.com/web/apps/data_center/pageIcon.png",
      disabledChangeForm: false,
      disabledChangeRole: false,
    };
  },
  created() {

    document.title = "登录职业大全";
    let SecretKey = this.$route.query.SecretKey;
    if (SecretKey) {
      localStorage.setItem("SecretKey", SecretKey);
      //获取客户信息
      loginCustomerInfo({ SecretKey }).then((res) => {
        if (res.code === 200) {
          this.customerName = res.data.customerName;
          this.customerDesc = res.data.customerDesc;
          const extra = res.data.extra;
          if (extra) {
            const extraObj = JSON.parse(extra);
            this.logoSrc = extraObj.picture;
          }
          localStorage.setItem("customerName", this.customerName);
          localStorage.setItem("customerDesc", this.customerDesc);
          localStorage.setItem("cardType", res.data.cardType);
        }
      });
    } else {
      //console.log("没有key!!!");
    }
    const roleType = localStorage.getItem("roleType");
    if (roleType) {
      switch (roleType) {
        case "10": //代表学生
          this.changeRole("student");
          break;
        case "20": //代表老师
          this.changeRole("teacher");
          break;

        default:
          break;
      }
    }
  },
  computed: {
    formType() {
      return this.$store.state.user.formType;
    },
    SaaSformType() {
      return this.$store.state.user.SaaSformType;
    },
    role() {
      return this.$store.state.user.role;
    },
  },
  watch: {
    formType: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        this.handleFormChange(newValue);
      },
    },
    SaaSformType: {
      immediate: true,
      deep: true,
      handler(newValue, oldValue) {
        this.handleSaaSFormChange(newValue);
      },
    },
  },
  methods: {
    changeForm(type) {
      //500毫秒内最多获取一次；
      if (this.disabledChangeForm) return;
      if (this.role == "teacher") {
        this.$store.dispatch("user/setSaaSFormType", type);
      } else if (this.role == "student") {
        this.$store.dispatch("user/setFormType", type);
      }

      this.disabledChangeForm = true;
      setTimeout(() => {
        this.disabledChangeForm = false;
      }, 1000);
    },
    changeRole(type) {
      //300毫秒内最多获取一次；
      if (this.disabledChangeRole) return;
      this.$store.dispatch("user/setRole", type);
      if (this.role == "teacher") {
        this.handleSaaSFormChange(this.SaaSformType);
      } else if (this.role == "student") {
        this.handleFormChange(this.formType);
      }
      this.disabledChangeRole = true;
      setTimeout(() => {
        this.disabledChangeRole = false;
      }, 200);
    },
    //切换表单触发的回调 ↓
    handleFormChange(formType) {
      switch (formType) {
        case "register":
          this.formDesc = "注册账号";
          this.currentForm = registerForm;
          break;
        case "login":
          this.formDesc = "密码登录";
          this.currentForm = loginForm;
          break;
        case "forget":
          this.formDesc = "忘记密码";
          this.currentForm = forgetForm;
          break;
        case "smslogin":
          this.formDesc = "短信登录";
          this.currentForm = smsloginForm;
          break;
        default:
          break;
      }
    },
    handleSaaSFormChange(formType) {
      switch (formType) {
        case "login":
          this.formDesc = "密码登录";
          this.currentSaaSForm = SaaSloginForm;
          break;
        case "forget":
          this.formDesc = "忘记密码";
          this.currentSaaSForm = SaaSForgetForm;
          break;
        case "smslogin":
          this.formDesc = "短信登录";
          this.currentSaaSForm = SaaSsmsloginForm;
          break;
        default:
          break;
      }
    },
  },
};
</script>

<style lang="scss"></style>

<style lang="scss" scoped>
.login-container {
  position: relative;
  min-height: 100%;
  width: 100%;
  min-width: 1200px;
  // overflow: hidden;
  background-image: url("../../assets/login/loginBG.png");
  background-size: cover;
  background-repeat: no-repeat;
  background-position: center top;
  .title {
    display: flex;
    justify-content: center;
    width: 100%;
    min-width: 100%;
    font-family: "title-home-font";
    font-weight: 400;
    font-size: clamp(24px, 2.2vw, 44px);
    color: #ffffff;
    line-height: 35px;
    letter-spacing: 2px;
    padding-top: min(24px, 3vh);
  }
  .login-block {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    position: absolute;
    top: calc(22% + 34px);
    width: 100%;
    height: 607px;
    margin: 0 auto;
    .login-form {
      position: relative;
      width: 368px;
      padding: 32px;
      padding-bottom: 12px;
      background: #ffffff;
      border-radius: 4px;
      z-index: 22;
      height: auto;
      .role-switch {
        width: 220px;
        box-sizing: border-box;
        position: absolute;
        display: flex;
        top: -64px;
        left: calc(50% - 100px);
        border: 1px solid #5e6d84;
        border-radius: 4px;
        background: rgba(47, 89, 141, 0.2);
        box-shadow: 0px 2px 10px 0px rgba(14, 30, 108, 0.5);
        font-family: MicrosoftYaHei;
        font-size: 14px;
        color: #a9bed6;
        line-height: 19px;
        text-align: left;
        font-style: normal;
        backdrop-filter: blur(1.5px);
        .role-item {
          padding: 9px 22px;
          cursor: pointer;
          &.active {
            color: #ffffff;
            font-weight: bold;
            font-size: 18px;
          }
          &:first-of-type::before {
            position: absolute;
            top: 13px;
            left: 100px;
            content: "";
            width: 1px;
            height: 12px;
            border: 1px solid #5e6d84;
          }
        }
      }
      .form-info {
        .logo {
          display: flex;
          justify-content: center;
          align-items: center;
          margin-bottom: 39px;
          width: 100%;
          height: 45px;
          img {
            width: auto;
            height: 45px;
          }
          .default-logo-container {
            display: flex;
            justify-content: center;
            align-items: center;
            .default-logo {
              width: auto;
              height: 32px;
            }
            .logo-text {
              margin: -4px 0px 0px 10px;
              font-family: "title-home-font";
              font-size: 22px;
              color: #222222;
              line-height: 26px;
              text-align: left;
              font-style: normal;
              font-weight: 400;
            }
          }
        }
        .descContainer {
          display: flex;
          justify-content: space-between;
          align-items: flex-end;
          margin-bottom: 31px;
          .desc {
            // width: 80px;
            height: 26px;
            font-family: Helvetica Neue, Helvetica, PingFang SC,
              Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
            font-weight: bold;
            font-size: 20px;
            color: #222222;
            line-height: 26px;
            text-align: left;
            font-style: normal;
          }
          .otherEntrance {
            // width: 57px;
            height: 19px;
            font-family: Helvetica Neue, Helvetica, PingFang SC,
              Hiragino Sans GB, Microsoft YaHei, Arial, sans-serif;
            font-size: 14px;
            color: #666666;
            line-height: 19px;
            text-align: left;
            font-style: normal;
            cursor: pointer;
            &:hover {
              text-decoration: underline;
            }
          }
        }
      }
    }
    .login-flow {
      .flow-item {
        position: absolute;
        width: 625px;
        // height: 235px;
      }
      .left {
        left: 15%;
      }
      .top {
        top: 6px;
      }
      .right {
        right: 15%;
      }
      .bottom {
        bottom: 6px;
      }
      @media (max-width: 1440px) {
        .left {
          left: 50px;
        }
        .top {
          top: 6px;
        }
        .right {
          right: 50px;
        }
        .bottom {
          bottom: 6px;
        }
      }
    }
  }
}
/* 去掉 Chrome 浏览器的数字输入框箭头 */
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
::v-deep .el-input__inner::placeholder {
  color: #94969d;
}
::v-deep .el-button--primary {
  background-color: #0b6eff;
}
/* 去掉 Firefox 浏览器的数字输入框箭头 */
input[type="number"] {
  -moz-appearance: textfield;
}
@media (max-width: 1440px) {
  .login-block {
    top: 225px !important;
  }
}

@media (max-width: 1600px) {
  .login-block {
    .flow-item {
      width: 40% !important;
      height: auto;
    }
  }
}
</style>
